import java.io.Serializable;

public class Score implements Serializable {
    private static final long serialVersionUID = 1L;
    private Student student; // 关联学生
    private Course course;   // 关联课程
    private double score;    // 分数

    public Score(Student student, Course course, double score) throws ScoreException {
        this.student = student;
        this.course = course;
        setScore(score);
    }

    // Getter & Setter
    public Student getStudent() { return student; }
    public void setStudent(Student student) { this.student = student; }
    public Course getCourse() { return course; }
    public void setCourse(Course course) { this.course = course; }
    public double getScore() { return score; }

    // 分数范围校验（抛出自定义异常）
    public void setScore(double score) throws ScoreException {
        if (score < 0 || score > 100) {
            throw new ScoreException("分数必须在0~100之间！");
        }
        this.score = score;
    }

    // 计算单课程绩点（示例规则：90+为4.0，80-89为3.0，依此类推）
    public double getGradePoint() {
        if (score >= 90) return 4.0;
        else if (score >= 80) return 3.0;
        else if (score >= 70) return 2.0;
        else if (score >= 60) return 1.0;
        else return 0.0;
    }

    @Override
    public String toString() {
        return "Score{student=" + student.getName() + ", course=" + course.getCourseName() + ", score=" + score + "}";
    }
}